import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;

public class FlashCards extends Game implements ActionListener, KeyListener
{

	public FlashCards(String categories, String level) 
	{
		super(categories, level);
	}

    //	public JFrame frame;
	public JTextField answer;
	public JLabel question;
	public Image img;
	public JPanel panel;
	public MainWindow parent;
	public JButton next;
	public JLabel score;
	public int correctAnswers = 0;
	public int attempts;
	public JPanel buttonPanel;
	public JPanel scorePanel;
	public JPanel questionPanel;
	public JPanel answerPanel;
	public HashMap<String,String> words = super.wordMap;
	public ArrayList<String> questions;
	public int currentQuestion;
	public JPanel correctAnswer;
	public JLabel correctLabel;
	public JPanel restartPanel;
	public JButton restart;
	
	public void init()
	{
		this.setTitle("Swahili Flash Cards!!");
		answer = new JTextField(15);
		question = new JLabel("test position");
		panel = new JPanel();
		answerPanel = new JPanel();
		setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setSize(225,300);
		
		next = new JButton("Next Word");
		score = new JLabel("");
		correctAnswers = 0;
		attempts = 0;
		panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.setAlignmentX(.5f);
//		buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		scorePanel = new JPanel();
		scorePanel.setLayout(new BoxLayout(scorePanel, BoxLayout.PAGE_AXIS));
		scorePanel.setAlignmentX(.5f);
//		scorePanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
//		question.setLocation(300,300);
		questionPanel = new JPanel();
		questionPanel.setLayout(new BoxLayout(questionPanel, BoxLayout.X_AXIS));
		questionPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,100));
		questionPanel.setAlignmentX(.5f);
//		this.setLocation(600, 400);
		correctAnswer = new JPanel();
		correctLabel = new JLabel();
		correctLabel.setForeground(Color.red);
		correctLabel.setBorder(BorderFactory.createEmptyBorder(0,10,0,100));
		correctLabel.setAlignmentX(.5f);
		correctLabel.setLayout(new BoxLayout(correctLabel, BoxLayout.X_AXIS));
		restartPanel = new JPanel();
//		restartPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		restart = new JButton("restart");

		restart.addActionListener(this);
		next.addActionListener(this);
		answer.addKeyListener(this);

		this.setResizable(false);
		

//		questionPanel.add(Box.createRigidArea(new Dimension(30,0)));
		questionPanel.add(question);
		answerPanel.add(answer);
		buttonPanel.add(next);
		buttonPanel.add(restart);
		scorePanel.add(score);
//		correctAnswer.add(Box.createRigidArea(new Dimension(25,0)));
		correctAnswer.add(correctLabel);
//		restartPanel.add(Box.createRigidArea(new Dimension(25,0)));
//		restartPanel.add(restart);
		


		currentQuestion = 0;
		attempts = wordMap.size();
		questions = new ArrayList<String>();
		for(String str: wordMap.keySet())
			questions.add(str);
		answer.setText("");
		question.setText(questions.get(currentQuestion));
		score.setText("Score: " + Integer.toString(correctAnswers)+"/"+Integer.toString(questions.size()));
		
		this.add(questionPanel);
		this.add(answerPanel);
		this.add(panel);
		this.add(buttonPanel);
		this.add(scorePanel);
		this.add(correctAnswer);
		this.add(restartPanel);

		restart.setVisible(false);
		
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		
	}

	public void restart()
	{
		correctLabel.setVisible(false);
		currentQuestion = 0;
		question.setText(questions.get(0));
		correctAnswers = 0;
		restart.setVisible(false);
		next.setVisible(true);
		draw();
	}

	public void nextWord()
	{	
		if(currentQuestion<questions.size()-1)
		{
			correctLabel.setVisible(false);
			check();
			answer.setText("");
			currentQuestion++;
			draw();
		}
		else 
		{
			check();
			question.setText("Final Score: "+ Integer.toString(correctAnswers)+"/"+Integer.toString(questions.size())+"!");
			this.setScore( (int)(((double) correctAnswers/questions.size())*100) );
			score.setText("");
			restartPanel.setVisible(true);
			restart.setVisible(true);
			next.setVisible(false);
		}

	}

	public void check()
	{
		if(wordMap.get(questions.get(currentQuestion)).equals(answer.getText()))
		{
			correctAnswers++;
			correctLabel.setVisible(false);
		}
		else
		{
			correctLabel.setText("correct answer: "+ wordMap.get(questions.get(this.currentQuestion)));
			correctLabel.setVisible(true);
		}

	}	
	public void draw()
	{
		answer.setText("");
		question.setText(questions.get(currentQuestion));
		score.setText("Score: " + Integer.toString(correctAnswers)+"/"+Integer.toString(questions.size()));

	}

/*	public static void main(String [] args){

		ArrayList<String> a = new ArrayList<String>();
		ArrayList<String> b = new ArrayList<String>();
		b.add("k");
		a.add("k");
		DB d = new DB();

		FlashCards f = new FlashCards("animals", "1");
		f.init(f.wordMap);
		System.out.println(f.wordMap.entrySet());
	}*/

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getActionCommand().equals("Next Word"))
			nextWord();
		else
		{
			restart();
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyChar() == '\n')
			nextWord();

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
}
